//
//  LinkPageViewController.m
//  TongLeLe
//
//  Created by XDD on 16/4/8.
//  Copyright © 2016年 XDD. All rights reserved.
//

#import "LinkPageViewController.h"
#import "TabBarViewController.h"

@interface LinkPageViewController ()<UIScrollViewDelegate> {
    UIScrollView *scrollView;
    NSTimer *timer_;
    BOOL isFromStart_;
    UIPageControl *pageControl_;
    UIImageView *imageview;
}

@end

@implementation LinkPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initGuide];   //加载新用户指导页面
}

- (void)handlePan{

    TabBarViewController *wei = [[TabBarViewController alloc]init];
    [self presentViewController:wei animated:YES completion:nil];

}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
    //UIStatusBarStyleDefault = 0 黑色文字，浅色背景时使用
    //UIStatusBarStyleLightContent = 1 白色文字，深色背景时使用
}

- (BOOL)prefersStatusBarHidden {
    
    return YES; //隐藏状态栏
}

- (void)initGuide
{
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    //     scrollView.backgroundColor = [UIColor redColor];
    [scrollView setContentSize:CGSizeMake([UIScreen mainScreen].bounds.size.width*3, 0)];
    scrollView.delegate = self;
    scrollView.showsHorizontalScrollIndicator = NO;
    [scrollView setPagingEnabled:YES];  //视图整页显示
    
    //    [scrollView setBounces:NO]; //避免弹跳效果,避免把根视图露出来
    for (int i = 0; i < 3; i++) {
        imageview = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width*i, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
          imageview.userInteractionEnabled = YES;
        imageview.image = [UIImage imageNamed:[NSString stringWithFormat:@"yindao%d.jpg",i +1]];
        [scrollView addSubview:imageview];
    }
    if ((imageview.image  = [UIImage imageNamed:@"yindao3.jpg"])) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        button.frame = CGRectMake(100, kHeight - 130, kWidth - 200, 35);
        [button setImage:[UIImage imageNamed:@"yindao_btn_img.jpg"] forState:UIControlStateNormal];
//        button.layer.cornerRadius = 4.0;
//        button.layer.masksToBounds = YES;
//        [button setTitle:@"跳过广告" forState:UIControlStateNormal];
//        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        [imageview addSubview:button];
        imageview.userInteractionEnabled = YES;
//        self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
        
//        self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
        
        UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan)];
             [imageview addGestureRecognizer:recognizer];
        
//        [imageview addGestureRecognizer:self.leftSwipeGestureRecognizer];
        
      
    }
    
    [self.view addSubview:scrollView];
    timer_ = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollPages:) userInfo:nil repeats:YES];
    
    // 页面指示器
    CGRect frame = self.view.frame;
    frame.origin.x = (frame.size.width - [UIScreen mainScreen].bounds.size.width) / 2;
    frame.origin.y = scrollView.frame.origin.y + [UIScreen mainScreen].bounds.size.height - 20;
    frame.size.width = [UIScreen mainScreen].bounds.size.width;
    frame.size.height = 20;
    pageControl_ = [[UIPageControl alloc] initWithFrame:frame];
    pageControl_.hidesForSinglePage = YES;
    pageControl_.userInteractionEnabled = YES;
    pageControl_.backgroundColor = [UIColor clearColor];
    pageControl_.currentPage = 0;
    pageControl_.numberOfPages = 3;
    [pageControl_ addTarget:self action:@selector(pageControlValueChangedAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:pageControl_];
}

- (IBAction)button:(id)sender {

    
    TabBarViewController *wei = [[TabBarViewController alloc]init];
    [self presentViewController:wei animated:YES completion:nil];
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    CGFloat pageWidth = sender.frame.size.width;
    int page = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl_.currentPage = page;
}

- (IBAction)pageControlValueChangedAction:(id)sender {
    UIPageControl *pageControl = sender;
    NSInteger currentPage = pageControl.currentPage;
    
    CGRect frame = scrollView.bounds;
    frame.origin.x = frame.size.width * currentPage;
    [scrollView scrollRectToVisible:frame animated:YES];
 
}

- (IBAction)scrollPages:(id)sender {
    ++pageControl_.currentPage;
    CGFloat pageWidth = CGRectGetWidth(scrollView.frame);
    if (isFromStart_) {
        [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        pageControl_.currentPage = 0;
    }
    else
    {
        [scrollView setContentOffset:CGPointMake(pageWidth*pageControl_.currentPage, scrollView.bounds.origin.y)];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
